package com.fujin.gulimall.product.controller;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fujin.gulimall.product.Vo.AttrResponseVo;
import com.fujin.gulimall.product.Vo.AttrVo;
import com.fujin.gulimall.product.entity.ProductAttrValueEntity;
import com.fujin.gulimall.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fujin.gulimall.product.entity.AttrEntity;
import com.fujin.gulimall.product.service.AttrService;
import com.fujin.common.utils.PageUtils;
import com.fujin.common.utils.R;



/**
 * 商品属性
 *
 * @author fujin
 * @email 1242257@gmail.com
 * @date 2021-12-25 10:56:06
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 修改sku规格信息
     * @param spuId
     * @param entities
     * @return
     */
    //product/attr/update/{spuId}
    @RequestMapping("/update/{spuId}")
    public R updateSpu(@PathVariable("spuId") Long spuId
            ,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpu(spuId,entities);

        return R.ok();
    }


    /**
     * 获取sku规格信息
     * @param spuId
     * @return
     */
    ///product/attr/base/listforspu/{spuId}
    @RequestMapping("/base/listforspu/{spuId}")
    public R listforSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> list = attrService.listCatelogById(spuId);

        return R.ok().put("data", list);
    }


    /**
     * 获取属性表数据，并关联得到分类&属性分组名称
     */
    @RequestMapping("/{type}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R baseList(@RequestParam Map<String, Object> params
            ,@PathVariable("catelogId") Long catelogId
            ,@PathVariable("type") String type){
        PageUtils page = attrService.queryBasePage(params,catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 回显三级分类及属性分组信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        AttrResponseVo attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改属性信息，并修改关联表
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrResponseVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
